import 'dart:async';

import 'package:enjoy_plus/utils/Toast.dart';
import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  // 状态变量
  int _count = 0;
  // 倒计时
  _countDownTime() {
    // 正在倒计时 阻断
    if (_count > 0) {
      return;
    }
    // 还没倒计时 继续执行-开启倒计时
    // 设置 初始时间
    setState(() {
      _count = 10;
    });
    Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {
        _count--;
        // 倒计时结束
        if (_count <= 0) {
          timer.cancel();
        }
      });
    });
  }

  // 输入框的控制器 - 手机号
  final mobileController = TextEditingController();
  // 输入框的控制器 - 验证码
  final codeController = TextEditingController();

  // 释放控制器
  @override
  void dispose() {
    mobileController.dispose();
    codeController.dispose();
    super.dispose();
  }

  // 获取验证码
  _getCode() {
    // .text 可以对输入框中的数据进行 修改，获取
    // ToastUtil.showInfo(mobileController.text);
    // ToastUtil.showInfo(codeController.text);
    codeController.text = '';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: const Text('登录'),
          backgroundColor: Colors.transparent,
          centerTitle: true,
        ),
        body: Container(
            padding: const EdgeInsets.all(20),
            child:
                Column(mainAxisAlignment: MainAxisAlignment.start, children: [
              const Row(
                children: [
                  Text('登录',
                      style:
                          TextStyle(fontSize: 25, fontWeight: FontWeight.bold))
                ],
              ),
              const SizedBox(height: 10),
              const Row(
                children: [
                  Text('加入享+, 让生活更轻松', style: TextStyle(fontSize: 15))
                ],
              ),
              const SizedBox(height: 30),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: TextField(
                      controller: mobileController,
                      decoration: const InputDecoration(
                        labelText: '手机号',
                        hintText: '请输入手机号',
                      ),
                    ),
                  ),
                  ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.white,
                      shadowColor: Colors.transparent,
                      foregroundColor: const Color.fromARGB(255, 85, 145, 175),
                      minimumSize: const Size(100, 50),
                    ),
                    onPressed: () {
                      _countDownTime();
                      // 获取验证码
                      _getCode();
                    },
                    child: _count > 0
                        ? Text('$_count 秒后重新获取')
                        : const Text('获取验证码'),
                  ),
                ],
              ),
              const SizedBox(height: 8),
              TextField(
                controller: codeController,
                decoration: const InputDecoration(
                  labelText: '验证码',
                  hintText: '请输入6位验证码',
                ),
              ),
              const SizedBox(height: 8),
              const Row(children: [
                Text('未注册手机号经验证后将自动登录',
                    style: TextStyle(fontSize: 12, color: Colors.grey)),
              ]),
              const SizedBox(height: 50),
              Row(children: [
                Expanded(
                    child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color.fromARGB(255, 85, 145, 175),
                    minimumSize: const Size(100, 50),
                  ),
                  onPressed: () {},
                  child: const Text('登录',
                      style: TextStyle(color: Colors.white, fontSize: 20)),
                ))
              ])
            ])));
  }
}
